package com.acwer.framework.common;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 并行执行器
 * @author ZhaoJie
 *
 */
public class ParallelExecutor {
	
	public static interface Action{
		public void run();
	}
	
	CountDownLatch latch;
	private List<Action> actions;
	ExecutorService pool;
	public ParallelExecutor(List<Action> actions){
		this.actions=actions;
		latch=new CountDownLatch(actions.size());
		pool=Executors.newFixedThreadPool(actions.size()+5);
		
	}
	public void run(){
		for (Action action : actions) {
			pool.execute(new Runnable() {
				public void run() {
					action.run();;
					latch.countDown();
				}
			});
		}
		pool.shutdown();
		try {
			latch.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
